<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PDF在线预览</title>

    <!-- 引入Bootstrap 5.3.0 CSS -->
    <link th:href="@{//cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css}" rel="stylesheet">

    <style>
        .container {
            max-width: 1200px;
            margin: 20px auto;
            padding: 20px;
        }
        .preview-container {
            margin-top: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
            padding: 15px;
            background-color: #f9f9f9;
            min-height: 500px;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .preview-placeholder {
            color: #666;
            text-align: center;
        }
        .pdf-image {
            max-width: 100%;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        .loading-spinner {
            display: none;
            width: 3rem;
            height: 3rem;
        }
    </style>
</head>
<body>
<div class="container">
    <h2 class="mb-4">PDF在线预览</h2>

    <!-- URL输入区域 -->
    <div class="card mb-4">
        <div class="card-body">
            <h5 class="card-title">PDF文件地址</h5>
            <div class="input-group">
                <input type="text" class="form-control" id="pdfUrl"
                       placeholder="请输入Minio PDF文件URL" th:value="${pdfUrl}">
                <button class="btn btn-primary" type="button" id="previewBtn">
                    <span class="spinner-border spinner-border-sm loading-spinner" aria-hidden="true"></span>
                    <span role="status">预览PDF</span>
                </button>
            </div>
            <div class="form-text">请输入Minio存储中的PDF文件URL地址</div>
        </div>
    </div>

    <!-- PDF预览区域 -->
    <div class="card">
        <div class="card-body">
            <h5 class="card-title">PDF预览</h5>
            <div class="preview-container">
                <div class="preview-placeholder" id="previewPlaceholder">
                    <p>请输入PDF URL并点击预览按钮</p>
                </div>
                <img id="pdfImage" class="pdf-image" style="display: none;">
            </div>
        </div>
    </div>
</div>

<!-- 引入jQuery 3.6.0 -->
<script th:src="@{//code.jquery.com/jquery-3.6.0.min.js}"></script>
<!-- 引入Bootstrap 5.3.0 JS -->
<script th:src="@{//cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js}"></script>

<script>
    $(document).ready(function() {
        // 预览按钮点击事件
        $('#previewBtn').click(function() {
            const pdfUrl = $('#pdfUrl').val().trim();

            if (!pdfUrl) {
                alert('请输入PDF文件URL');
                return;
            }

            // 显示加载状态
            $('.loading-spinner').show();
            $('#previewBtn').prop('disabled', true);

            // 调用后端API
            $.ajax({
                url: '/pdf/render',
                type: 'POST',
                data: {
                    pdfUrl: pdfUrl,
                    pageIndex: 0  // 默认预览第一页
                },
                xhrFields: {
                    responseType: 'blob'  // 重要：指定响应类型为blob[8](@ref)
                },
                success: function(data) {
                    // 创建对象URL
                    const imageUrl = URL.createObjectURL(data);

                    // 显示图片
                    $('#pdfImage').attr('src', imageUrl).show();
                    $('#previewPlaceholder').hide();

                    // 恢复按钮状态
                    $('.loading-spinner').hide();
                    $('#previewBtn').prop('disabled', false);
                },
                error: function(xhr, status, error) {
                    console.error('预览失败:', error);
                    alert('预览失败: ' + (xhr.responseJSON?.message || '未知错误'));

                    // 恢复按钮状态
                    $('.loading-spinner').hide();
                    $('#previewBtn').prop('disabled', false);
                }
            });
        });

        // 支持按Enter键触发预览
        $('#pdfUrl').keypress(function(e) {
            if (e.which === 13) { // Enter键
                $('#previewBtn').click();
            }
        });
    });
</script>
</body>
</html>